﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using SubSonic.Schema;
using WebeTerm.DbProvider;
using WebeTerm.Model;
using WebeTerm.Common;

namespace WebeTerm.DbBusiness
{
    
    public class BaseBLL<T> where T : class ,new()
    {
        protected BaseDAL<T> provider = null;
        protected BaseBLL(BaseDAL<T> dbProvider)
        {
            provider = dbProvider;
            CurrentDate = DateTime.Now;
        }

        public DateTime CurrentDate { get; private set; }

        /// <summary>
        /// 通过主键获取实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual T GetOriginalModel(T model) {
            return new T();
        }

        public BaseDAL<T> Provider { get { return provider; } }

        public void Add(T model)
        {
            provider.Add(model);
            //DbBusinessHelpler.Instance.Insert(model, this);
        }

        public bool Exists(Expression<Func<T, bool>> expression) {
            return provider.All().Where(expression).Any();
        }

        public void Save(T model) {
            if (model is IModel) { 
                IModel entity = model as IModel;
                if (entity.IsLoaded())
                    Update(model);
                else
                    Add(model);
            }
        }

        public List<T> Query(IQueryable<T> iQuery, Pages pages)
        {
            var items = provider.SplitPage(iQuery.Distinct(), pages).ToList();
            items.ForEach(l =>
            {
                { if (l is IModel) { (l as IModel).SetIsLoaded(); } }
            });
            return items;
        }

        /// <summary>
        /// 业务对象
        /// </summary>
        public string ObjectCode { set; internal get; }

        public void Update(T model)
        {
            T originalModel =  GetOriginalModel(model);
            provider.Update(model);
            //DbBusinessHelpler.Instance.Update(model,originalModel,this);
        }

        public void Update(IEnumerable<T> items) {
            //provider.Update(items);
            foreach (T item in items)
                Update(item);
        }

        public void Delete(T model)
        {
            T originalModel = GetOriginalModel(model);
            provider.Delete(model);
            //DbBusinessHelpler.Instance.Delete(originalModel, this);
        }

        public T FirstOrDefault(Expression<Func<T, bool>> expression)
        {
            return provider.FirstOrDefault(expression);
        }

        public IQueryable<T> All()
        {
            return provider.All();
        }

        public PagedList<T> GetPaged<TKey>(Func<T, TKey> orderBy, Expression<Func<T, bool>> expression, int pageIndex, int pageSize)
        {
            return provider.GetPaged<TKey>(orderBy, expression, pageIndex, pageSize);
        }

        public PagedList<T> GetPaged(string sortBy, int pageIndex, int pageSize)
        {
            return provider.GetPaged(sortBy, pageIndex, pageSize);
        }

        public IQueryable<T> Query(Expression<Func<T, bool>> expression)
        {
            return All().Where(expression);
        }


        public List<Q> QueryLinq<Q>(IQueryable<Q> iQuery, Pages pages){
            return provider.SplitPage<Q>(iQuery, pages);
        }

        public int DeleteMany(Expression<Func<T, bool>> expression)
        {
            var items = All().Where(expression).ToList();
            foreach (T item in items)
                Delete(item);
            return items.Count;
        }

        public void Add(IEnumerable<T> items)
        {
            //provider.Add(items);
            foreach (T item in items)
                Add(item);
        }

    }
}
